﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using WcfServices.ServiceInterfaces;
using WcfServices.Utilities;
using BLL;
using EntitiesLayer;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Drawing;
using System.IO;


namespace WcfServices
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CitasService: ICitaService
    {
        private GestorCita gestor;


        public CitasService() {

            gestor = new GestorCita();
        
        }
        public string Listar()
        {
            IEnumerable<Cita> listaCitas =
                Util.interceptWithReturn(gestor.listarCitas);

            if (listaCitas != null)
                return listaCitas.JsonSerialize();

            return "";
        }

        public string BuscarPorId(int id)
        {
            Cita cita =
               Util.interceptWithReturn(() => gestor.BuscarCitaPorId(id));

            if (cita != null)
                //return JsonConvert.SerializeObject(maquina, new StringEnumConverter());
                return cita.JsonSerialize();

            return "";
        }

        public void Create(Stream json)
        {
            IDictionary<string, string> data = json.DeserializeJson<Dictionary<string, string>>();

            Util.interceptWithNoReturn(
                () =>
                gestor.crearCita(Convert.ToInt32(data["IdTipoCita"]),Convert.ToDateTime(data["FechaHoraCita"]), Convert.ToInt32(data["IdEncargado"]),
                Convert.ToInt32(data["IdCliente"]), Convert.ToString(data["Notas"]), Convert.ToChar(data["estado"]))
            );
        }

        public void Delete(int pid)
        {
            gestor.EliminarCita(pid);
        }

        public void Update(Stream json)
        {
            dynamic data = json.DeserializeJson<dynamic>();
            Util.interceptWithNoReturn(
                () =>
                gestor.ModificarCita(Convert.ToInt32(data["IdCita"]),Convert.ToInt32(data["IdTipoCita"]), Convert.ToDateTime(data["FechaHoraCita"]), Convert.ToInt32(data["IdEncargado"]),
                Convert.ToInt32(data["IdCliente"]), Convert.ToString(data["Notas"]), Convert.ToChar(data["estado"]))
            );

        }
    }
}
